package com.haidechizi.longpolling.global;

import com.haidechizi.longpolling.client.entity.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

/**
 * @author wangdejun
 * @create 2021/2/24 14:59
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(DuplicateKeyException.class)
    public Response handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return Response.builder().error("数据库中已存在该记录");
    }

    @ExceptionHandler(BindException.class)
    public Response handleBindException(BindException e) {
        log.error(e.getMessage(), e);
        return Response.builder().error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Response.builder().error();
    }

}
